package com.xiaoq.sitesticket.repository.impl;

import com.xiaoq.sitesticket.entity.SitesTicketEntity;
import com.xiaoq.sitesticket.repository.ISitesTicketRepository;
import com.xiaoq.tool.util.IdGeneratorUtil;
import org.apache.commons.lang3.StringUtils;
import org.mongodb.morphia.Datastore;
import org.mongodb.morphia.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

@Repository
public class SitesTicketRepositoryImpl implements ISitesTicketRepository {

    @Autowired
    private Datastore dataStore;

    @Override
    public SitesTicketEntity findSitesTicketById(String id) {
        if(StringUtils.isNotBlank(id)){
            Query<SitesTicketEntity> sitesTicketQuery = dataStore.find(SitesTicketEntity.class).field(IdGeneratorUtil.IDGENERATOR).equal(id);
            return sitesTicketQuery.get();
        }
        return null;
    }

    @Override
    public List<SitesTicketEntity> findSitesTicketListBySitesId(String sitesId) {
        if(StringUtils.isNotBlank(sitesId)){
            Query<SitesTicketEntity> sitesTicketQuery = dataStore.find(SitesTicketEntity.class).field("sitesId").equal(sitesId);
            return sitesTicketQuery.asList();
        }
        return new ArrayList<>();
    }
}
